package com.test.qykj.qyelectromobile.ui.base;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ProgressBar;

import com.blankj.utilcode.util.ToastUtils;
import com.test.qykj.qyelectromobile.App;
import com.test.qykj.qyelectromobile.R;
import com.test.qykj.qyelectromobile.utils.HttpUtil;

import org.xutils.http.RequestParams;


/**
 * Created by Erebus on 2016/8/19.
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected final String TAG = this.getClass().getSimpleName();
    protected Toolbar toolbar;
    protected App application;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        application = (App) getApplicationContext();
//        if (this instanceof WelcomeActivity)
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        else {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        }
        initView();
        setupWindowAnimations();
        initToolBar();
        initListener();
        initData();
    }

    protected void setupWindowAnimations() {
        setupEnterAnimations();
        setupReenterAnimations();
        setupExitAnimations();
    }

    protected void setupEnterAnimations() {

    }

    protected void setupReenterAnimations() {

    }

    protected void setupExitAnimations() {

    }


    protected void postRequest(RequestParams params, int action, ProgressBar progressBar) {

        HttpUtil.httpPost(params, action, new HttpUtil.HttpRequestCallback() {
            @Override
            public void onSuccess(int action, String result) {
                if (!BaseActivity.this.isFinishing()) {
                    onResponseSuccess(action, result);
                }
            }

            @Override
            public void onFailure(int action, Throwable e, String s) {
                if (!BaseActivity.this.isFinishing()) {
                    onResponseFailure(action, e, s);
                }
            }
        }, progressBar);
    }

    protected abstract void onResponseSuccess(int action, String response);

    protected abstract void onResponseFailure(int action, Throwable e, String s);

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }


    /**
     * 初始化toolBar
     */
    public void initToolBar(boolean isShowBackBtn) {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle("");
            setSupportActionBar(toolbar);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayHomeAsUpEnabled(isShowBackBtn);
            }
        }
    }

    /**
     * 初始化toolBar
     */
    public void initToolBar(boolean isShowBackBtn, int title) {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle(title);
            setSupportActionBar(toolbar);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayHomeAsUpEnabled(isShowBackBtn);
            }
        }
    }

    /**
     * 初始化toolBar
     */
    public void initToolBar(boolean isShowBackBtn, String title) {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle(title);
            setSupportActionBar(toolbar);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayHomeAsUpEnabled(isShowBackBtn);
            }
        }
    }

    /**
     * 初始化toolBar，控件
     */
    protected abstract void initToolBar();

    /**
     * 初始化界面，控件
     */
    protected abstract void initView();

    /**
     * 初始化控件监听器
     */
    protected abstract void initListener();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 提示
     *
     * @param msg
     */
    protected void showToastShort(CharSequence msg) {
        ToastUtils.showShortSafe(msg);
    }
    protected void showToastLong(CharSequence msg) {

        ToastUtils.showLongSafe(msg);
    }
}
